package org.smart4j.framework;

import org.smart4j.framework.Helper.BeanHelper;
import org.smart4j.framework.Helper.ControllerHelper;
import org.smart4j.framework.Helper.ServletHelper;
import org.smart4j.framework.bean.Data;
import org.smart4j.framework.bean.Handler;
import org.smart4j.framework.bean.Param;
import org.smart4j.framework.bean.View;
import org.smart4j.framework.util.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by feng.ding on 2016/9/23.
 */
@WebServlet(urlPatterns = "/*", loadOnStartup = 0)
public class DispatcherServlet extends HttpServlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //初始化相关helper类
        HelperLoader.init();
        ServletContext servletContext = servletConfig.getServletContext();
        //注册处理JSP的Servlet
        ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
        //注册处理静态资源的默认servlet
        ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
        defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletHelper.init(req, resp);
        try{
            String requestMethod = req.getMethod().toLowerCase();
            String requestPath = req.getPathInfo();
            Handler handler = ControllerHelper.getHandler(requestMethod, requestPath);
            if(handler != null){
                Class<?> controllerClass = handler.getControllerClass();
                Object controllerBean = BeanHelper.getBean(controllerClass);
                //创建请求参数对象
                Map<String, Object> paramMap = new HashMap<String, Object>();
                Enumeration<String> paramNames = req.getParameterNames();
                while (paramNames.hasMoreElements()){
                    String paramName = paramNames.nextElement();
                    String paramValue = req.getParameter(paramName);
                    paramMap.put(paramName, paramValue);
                }
                String body = CodecUtil.decodeURL(StreamUtil.getString(req.getInputStream()));
                if(StringUtil.isNotEmpty(body)){
                    String[] params = StringUtil.splitString(body, "&");
                    if(ArrayUtil.isNotEmpty(params)){
                        for(String param : params){
                            String[] array = StringUtil.splitString(param, "=");
                            if(ArrayUtil.isNotEmpty(array) && array.length == 2){
                                String paramName = array[0];
                                String paramValue = array[1];
                                paramMap.put(paramName, paramValue);
                            }
                        }
                    }
                }
                Param param = new Param(paramMap);
                //调用Action方法
                Method actionMethod = handler.getActionMethod();
                Object result;
                if(param.isEmpty()){
                    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod);
                }else{
                    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
                }
                //处理action返回值
                if(result instanceof View){
                    View view = (View)result;
                    String path = view.getPath();
                    if(StringUtil.isNotEmpty(path)){
                        if(path.endsWith("/")){
                            resp.sendRedirect(req.getContextPath() + path);
                        }else {
                            Map<String, Object> model = view.getModel();
                            for(Map.Entry<String, Object> entry : model.entrySet()){
                                req.setAttribute(entry.getKey(), entry.getValue());
                            }
                            req.getRequestDispatcher(ConfigHelper.getAppJspPath() + path).forward(req, resp);
                        }
                    }
                }else if(result instanceof Data){
                    //返回Json数据
                    Data data = (Data) result;
                    Object model = data.getModel();
                    if(model != null){
                        resp.setContentType("appliction/json");
                        resp.setCharacterEncoding("utf-8");
                        PrintWriter writer = resp.getWriter();
                        String json = JsonUtil.toJson(model);
                        writer.write(json);
                        writer.flush();
                        writer.close();
                    }
                }
            }
        } finally {
            ServletHelper.destory();
        }

    }
}
